package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.CardDao;
import com.example.child_protection.dto.base.BaseResponseDTO;
import com.example.child_protection.dto.info.CardDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.add.CardInsertRequestDTO;
import com.example.child_protection.dto.request.find.CardRequestDTO;
import com.example.child_protection.dto.request.find.GetCardRequestDTO;
import com.example.child_protection.dto.response.CardListResponseDTO;
import com.example.child_protection.dto.response.CardResponseDTO;
import com.example.child_protection.enums.BooleanEnums;
import com.example.child_protection.enums.CardStatesEnums;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.CardModel;
import com.example.child_protection.service.CardManageService;
import com.example.child_protection.service.CardQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class CardQueryServiceImpl extends MyBaseService implements CardQueryService {

    @Autowired
    public CardDao cardDao;

    @Autowired
    public CardManageService cardManageService;

    /**
     * 获取一个未使用的卡号
     *cc
     * @param requestDTO
     * @return
     */
    public CardResponseDTO getCardNo(GetCardRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CARD_FIND, requestDTO, CardResponseDTO.class, new MyServiceExecutor<GetCardRequestDTO, CardResponseDTO>() {
            @Override
            protected CardResponseDTO myprocess(GetCardRequestDTO requestDTO) throws Exception {
                CardResponseDTO responseDTO = new CardResponseDTO();
                CardModel condition = new CardModel();
                condition.setStates(CardStatesEnums.NOT_ACTIVE);
                condition.setIsMadeCard(BooleanEnums.F);
                int count = cardDao.count(condition);
                //没有满足条件的卡号，则新增
                if( 0 == count ){
                    CardInsertRequestDTO create = new CardInsertRequestDTO();
                    create.setCreatePersonId(10000L);
                    create.setLastUpdatePersonId(10000L);
                    create.setNums(100);
                    BaseResponseDTO responseDto =  cardManageService.createCardBatch(create);
                }
                CardModel cardModel = cardDao.getCardNo(condition);
                CardDTO card = new CardDTO();
                BeanUtils.copyProperties(cardModel, card);
                cardModel.setStates(CardStatesEnums.READY_TO_ACTIVE);
                //修改卡号使用状态为准备激活
                cardDao.updateSelective(cardModel);
                responseDTO.setInfo(card);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public CardListResponseDTO findList(CardRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.CARD_FIND_LIST, requestDTO, CardListResponseDTO.class, new MyServiceExecutor<CardRequestDTO, CardListResponseDTO>() {
            @Override
            protected CardListResponseDTO myprocess(CardRequestDTO requestDTO) throws Exception {
                CardListResponseDTO responseDTO = new CardListResponseDTO();
                CardModel req = new CardModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<CardModel> lists = cardDao.findPage(req);
                List<CardDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), CardDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
